package dyy.zc.alumnibook.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import cn.bmob.v3.datatype.BmobFile;
import dyy.zc.alumnibook.R;
import dyy.zc.alumnibook.listener.OnItemFriendClick;
import dyy.zc.alumnibook.utils.ImageLoaderUtils;
import dyy.zc.alumnibook.utils.Utils;

/**
 * Created by chuan on 2017/4/15.
 */

public class FriendListAdapter extends RecyclerView.Adapter<FriendListAdapter.MyViewHolder>{
    private Context context;
    private List<String> nameList;
    private List<BmobFile> headList;
    private OnItemFriendClick onItemFriendClick;
    private int parent = -1;
    public FriendListAdapter(Context context, List<BmobFile> headList, List<String> nameList,int parent,OnItemFriendClick onItemFriendClick){
        this.context = context;
        this.headList = headList;
        this.nameList = nameList;
        this.parent = parent;
        this.onItemFriendClick = onItemFriendClick;
    }

    public void setList(List<BmobFile> headList,List<String> nameList) {
        this.headList = headList;
        this.nameList = nameList;
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_friend,null),onItemFriendClick);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        ImageLoaderUtils.initImage(context,headList.get(position).getFileUrl(),
                holder.head,R.drawable.chat_xe_icon2_03);
        holder.name.setText(nameList.get(position));
    }

    @Override
    public int getItemCount() {
        if (headList == null || headList.size() == 0){
            return 0;
        }
        return headList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private OnItemFriendClick onItemFriendClick;
        private ImageView head;
        private TextView name;
        public MyViewHolder(View itemView,OnItemFriendClick onItemFriendClick) {
            super(itemView);
            itemView.setOnClickListener(this);
            this.onItemFriendClick = onItemFriendClick;
            head = Utils.findViewsById(itemView,R.id.head);
            name = Utils.findViewsById(itemView,R.id.name);
        }

        @Override
        public void onClick(View v) {
            if (onItemFriendClick !=null){
                onItemFriendClick.onFriendClick(parent,getAdapterPosition());
            }
        }
    }
}
